import { defineStore } from "pinia";
const useDictStore = defineStore("dict", {
    state: () => ({
        dictKey: new Set(),
        dict: <any>[],
    }),
    actions: {
        // 获取字典
        getDict(_key: string) {
            if (_key == null && _key == "") {
                return null;
            }
            try {
                for (let i = 0; i < this.dict.length; i++) {
                    if (this.dict[i].key == _key) {
                        return this.dict[i].value;
                    }
                }
            } catch (e) {
                return null;
            }
        },
        // 设置字典
        setDict(_key: string, value: any) {
            if (_key !== null && _key !== "") {
                this.dict.push({
                    key: _key,
                    value: value,
                });
                this.dictKey.add(_key);
            }
        },
        // 删除字典
        removeDict(_key: string) {
            let bln = false;
            try {
                for (let i = 0; i < this.dict.length; i++) {
                    if (this.dict[i].key == _key) {
                        this.dict.splice(i, 1);
                        this.dictKey.delete(_key);
                        return true;
                    }
                }
            } catch (e) {
                bln = false;
            }
            return bln;
        },
        // 清空字典
        cleanDict() {
            this.dict = [];
            this.dictKey.clear();
        },
        // 初始字典
        initDict(dicts: Array<any>, dictKeys: Array<any>) {
            this.dict = dicts;
            dictKeys.forEach((item) => {
                this.dictKey.add(item);
            });
        },
    },
});

export default useDictStore;
